var maxTime = 20 * 60;
var timer;
$().ready(function() {
    validateRule();
    upload();
    timer = setInterval("CountDown()", 1000);
});

$.validator.setDefaults({
    submitHandler : function() {
        save();
    }
});

//操作倒计时
function CountDown() {
    if(maxTime >= 0) {
        minutes = Math.floor(maxTime / 60);
        seconds = Math.floor(maxTime % 60);
        $('#down').text(minutes + "分 " + seconds + "秒");
        --maxTime;
    } else {
        clearInterval(timer);
        //调用后台修改当前充值订单状态
        saveDown();
    }
}

function saveDown() {
    $.ajax({
        cache : true,
        type : "POST",
        url : "/cash/cash/recharge/update",
        data : {
            id : $('#id').val()
        },// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("链接超时");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作超时，请重新操作");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(data.msg)
            }

        }
    });
}

//初始化上传控件
function upload(){
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var url = '/cash/cash/upload/'+ $('#id').val()
        var uploadInst = upload.render({
            elem: '#upload', //绑定元素
            url: url, //上传接口
            size: 5120,
            accept: 'file',
            done: function (r) {
                if (r.code == '0'){
                    $('#vouchers').val(r.data.voucher);
                    $('#voucherType').val(r.data.voucherType);
                    $('#moneys').val($('#money').val());

                    $('#upload').css('display', 'none');
                    $('#pz').css('display', 'block');

                    var type = r.data.voucherType;
                    var str = "data:image/" + (type == 'jpg' ? 'jpeg' : type) + ";base64," + r.data.vouchers;
                    $('#pz').attr('src', str);
                    layer.msg("文件上传成功");
                } else {
                    layer.msg(r.msg);
                }
            },
            error: function (r) {
                layer.msg(r.msg);
            }
        });
    });
}

function save() {
    // $('#bankNumbers').val($('#bankNumber').val());
    // $('#bankDeposits').val($('#bankDeposit').val());
    // $('#realNames').val($('#realName').val());
    $('#moneys').val($('#money').val());
    $.ajax({
        cache : true,
        type : "POST",
        url : "/cash/cash/rechargeSave",
        data : $('#signupForms').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("链接超时");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    var maxMoney = $('#countMoney').val()/1;
    $("#signupForm").validate({
        rules : {
            money : {
                required : true,
                max : maxMoney,
                min : 0
            },
            voucher : {
                required : true
            },
			code : {
                required : true,
                remote : {
                    url : "/google/check", // 后台处理程序
                    type : "post", // 数据发送方式
                    dataType : "json", // 接受数据格式
                    data : { // 要传递的数据
                        code : function() {
                            return $("#code").val();
                        }
                    }
                }
			}
        },
        messages : {
            money : {
                required : icon + "请输入充值金额",
                max : icon + "充值金额不能大于最大可充值金额",
                min : icon + "充值金额必须大于0"
            },
            voucher : {
                required : icon + "请上传凭证"
            },
			code : {
                required: icon + "请输入数字令牌",
                remote: icon + "数字令牌不正确"
			}
        }
    })
}
